﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace XRpgLibrary
{
    public class AIStateManager
    {
        public interface IaiStateOwner
        {
            float CurrentStateElapsed { get; set; }
        }
        public void addState(short key, AIState ai)
        {
            if (!_aiStates.ContainsKey(key))
                _aiStates.Add(key, ai);
        }
        public void addState(AIState ai)
        {
            addState(ai.key, ai);
        }
        public AIState CurrentState;
        public AIState PreviousState;
        public void Update(GameTime gt, IaiStateOwner owner)
        {
            CurrentState.Update(gt, owner);
        }
        public void Execute(object owner)
        {
            short _newKey = CurrentState.Execute(owner);
            if (_newKey == CurrentState.key || _newKey == 0)
                return;
            CurrentState.Exit(owner);
            CurrentState = _aiStates[_newKey];
            CurrentState.Enter(owner);

        }
        public class AIState
        {
            public short key = 0;
            public virtual void Update(GameTime gt, IaiStateOwner owner)
            {
                owner.CurrentStateElapsed += gt.ElapsedGameTime.Milliseconds / 1000f;
            }
            public virtual short Execute(object owner)
            {
                return 0;
            }
            public virtual void Enter(object owner)
            {

            }
            public virtual void Exit(object owner)
            {
                if ((owner as IaiStateOwner) != null)
                    (owner as IaiStateOwner).CurrentStateElapsed = 0;
            }
        }
        private AIState _previousState;
        private AIState _currentState;
        private Dictionary<short, AIState> _aiStates = new Dictionary<short, AIState>();
    }
}
